package com.pgy.backend.controller;

import com.pgy.backend.entity.Pointer;
import com.pgy.backend.mapper.PointerMapper;
import com.pgy.backend.service.impl.PointerService;
import com.pgy.backend.vo.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import static com.pgy.backend.utils.PointerNumberAndUsed.parse;

@RestController
@RequestMapping("/pointer")
@PreAuthorize("hasAuthority('root')")
@Api(tags = "指标相关接口")
public class PointerController {
    @Autowired
    PointerMapper pointerMapper;
    @Autowired
    PointerService pointerService;

    @GetMapping
    @ApiOperation(value = "获取全部的指标信息")
    public ResultData get(){
        return ResultData.success(pointerMapper.findAll());
    }


    @PostMapping("/save")
    @ApiImplicitParam(name = "pointer",value = "指标实体类")
    @ApiOperation(value = "保存新的指标")
    public ResultData save(@RequestBody Pointer pointer) {
        pointer.setPointerNumber(parse(pointer.getPointer_used()));
        return ResultData.success(pointerMapper.insert(pointer));

    }

    @PostMapping("/update")
    @ApiImplicitParam(name = "pointer",value = "指标实体类")
    @ApiOperation(value = "更新对应的指标信息")
    public ResultData update(@RequestBody Pointer pointer){
        //新增和更新
        return ResultData.success(pointerMapper.myupdateById(pointer));
    }

    @DeleteMapping("/{id}")
    @ApiImplicitParam(name = "id",value = "指标的ID")
    @ApiOperation(value = "删除指针")
    public ResultData delete(@PathVariable Integer id) {
        return ResultData.success(pointerService.removeById(id));
    }

    @GetMapping("/assign/{id}")
    public ResultData updateAssign(@PathVariable Integer id){
        return ResultData.success(pointerMapper.myupdateAssign(id));
    }
}
